package com.im.tcp.config;

import com.im.tcp.reciver.UserLoginMessageListener;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Autowired
    private TcpConfig nettyConfig;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.database}")
    private int database;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setDatabase(database);
        // 设置 Redisson 序列化/反序列化的编码方式
        StringCodec stringCodec = new StringCodec();
        config.setCodec(stringCodec);
        return Redisson.create(config);
    }

    @Bean
    public UserLoginMessageListener userLoginMessageListener() {
        UserLoginMessageListener userLoginMessageListener = new UserLoginMessageListener(redissonClient(), nettyConfig);
        userLoginMessageListener.listenerUserLogin();
        return userLoginMessageListener;
    }

}
